# barco/sandboxmoduledesc.py

import string

class SandboxModuleDescription(object):

    class Description(object):

        def __init__(self, parent):
            self.parent = parent
            return None

        def name(self):
            return self.parent.get_module()

        def Depends(self, dependecies):
            if isinstance(dependecies, list): self.parent.add_dependecies(dependecies)
            else: self.parent.add_dependecies([ dependecies ])
            return self

        def AddToClass(self, classes):
            return self

        def ExportGlobal(self, **argv):
            self.parent.add_exported_globals(argv)
            return self

        def ExportFlags(self, **argv):
            self.parent.add_exported_flags(argv)
            return self

        def get_dependecies(self):
            return self.dependecies

    class Barco(object):

        def __init__(self, parent):
            self.parent = parent
            return None

        def ModuleDescription(self):
            return SandboxModuleDescription.Description(self.parent)

    def __init__(self, module, data):            
        self.module = module
        # We are always dependend on srcrepos module unless we are srcrepos ourself.
        if module == 'srcrepos': self.dependecies = list()
        else: self.dependecies = [ 'srcrepos' ] 
        self.exported_globals = dict()
        self.exported_flags = dict()
        exec compile(data, '<desc:%s>' % module, 'exec') in {}, { 'Barco': SandboxModuleDescription.Barco(self) }
        return None

    def get_module(self):
        return self.module

    def add_dependecies(self, dependecies):
        self.dependecies.extend(dependecies)
        return None

    def add_exported_globals(self, exported_globals):
        for key in exported_globals.keys(): self.exported_globals[key] = string.Template(exported_globals[key]).safe_substitute(MODULE=self.module)
        return None

    def add_exported_flags(self, exported_flags):
        for key in exported_flags.keys(): self.exported_flags[key] = string.Template(exported_flags[key]).safe_substitute(MODULE=self.module)      
        return None

    def get_dependecies(self):
        return self.dependecies

    def get_exported_globals(self):
        return self.exported_globals

    def get_exported_flags(self):
        return self.exported_flags
    
